package jp.sourceforge.talisman.mds.distance;

/*
 * $Id: GroupAverageClusterDistanceCalculator.java,v 1.1 2009/09/12 05:06:07 weiwei Exp $
 */

import jp.sourceforge.talisman.mds.Item;
import jp.sourceforge.talisman.mds.cluster.Cluster;

/**
 * 
 * @author Haruaki Tamada
 * @version $Revision: 1.1 $ 
 */
public class GroupAverageClusterDistanceCalculator extends AbstractClusterDistanceCalculator{
    public GroupAverageClusterDistanceCalculator(){
    }

    public GroupAverageClusterDistanceCalculator(ItemDistanceAlgorithm ida){
        super(ida);
    }

    @Override
    public ClusterDistanceAlgorithm getAlgorithm(){
        return ClusterDistanceAlgorithm.GROUP_AVERAGE_METHOD;
    }

    @Override
    public double calculate(Cluster c1, Cluster c2){
        double sum = 0d;

        for(Item item1: c1){
            for(Item item2: c2){
                sum += getItemDistanceCalculator().calculate(item1, item2);
            }
        }
        return sum / (c1.getSize() * c2.getSize());
    }

    @Override
    public double update(Cluster ci, Cluster cj, Cluster ck, double dij, double dik, double djk){
        int sum = ci.getSize() + cj.getSize();

        return dik * ci.getSize() / sum + djk * cj.getSize() / sum;
    }
}
